<html>
<head>
  <title>How To Link Programs...</title>
</head>

<body>

<!-- BEGIN LAB DESCRIPTION -->

<font face="Arial">
<!------------------------------------------------------------------------------------>
<a name="HowToLinkPrograms">
<h2>How To Link Programs...</h2>
</a>

<p>Part of systemic decomposition usually involves dividing a program into separate files.
Smaller files are easier to maintain and support Object Oriented Programming (OOP) by
"hiding" or encapsulating data.  The problem of coming up with unique label names is mitigated.

<p>The Linker accepts the objects from many files, libraries and programs, and outputs an
executable program.  The compile/assembly/link process is controlled by a make script.

<p>Symbols and labels are made known to other files with the <b>.def</b> assembler directive.
For example:

<p><blockquote><pre>;------------------------------------------------------------------------------
;    external references
;
        .def    initRand16            ; init random # generator
        .def    rand16                ; get 16-bit random #
        .def    setRandSeed           ; set random # seed
        .def    getRandSeed           ; get current random # seed

;------------------------------------------------------------------------------
        .text                         ; Program Section
;------------------------------------------------------------------------------
; SUBROUTINE: INITIALIZE RANDOM NUMBER GENERATOR:
;
;    IN:        r12 = random seed
;
initRand16:
        push    r4
        mov.w   r12,RandSeed           ; init seed
        mov.w   #20,r4

init_01:
        call    #rand16                ; get random #
        dec.w   r4
          jne   init_01
        pop     r4
        ret

;------------------------------------------------------------------------------
; SUBROUTINE: SET RANDOM SEED

setRandSeed:
        mov.w   r12,RandSeed           ; set seed
        ret


;------------------------------------------------------------------------------
; SUBROUTINE: GET RANDOM SEED

getRandSeed:
        mov.w    RandSeed,r12           ; return seed
        ret                             ; Random number in Rndnum</pre></blockquote>

<p>Symbols and labels are referenced in other files with the <b>.ref</b> assembler directive.
For example:

<p><blockquote><pre>;------------------------------------------------------------------------------
;    external references
;
        .ref    initRand16              ; init random # generator
        .ref    rand16                  ; get 16-bit random #
        .ref    setRandSeed             ; set random # seed
        .ref    getRandSeed             ; get current random # seed

        mov.w   #SEED,r12               ; get good seed
        call    #setRandSeed            ; set current random seed
        bis.w   #GIE,SR                 ; enable interrupts

;------------------------------------------------------------------------------
;    start simon
;------------------------------------------------------------------------------
start:  call    #initRand16             ; Initialize generator

;------------------------------------------------------------------------------
;        next attempt, output LEDs
;------------------------------------------------------------------------------
next:   call    #newGame                ; start new game
        call    #getRandSeed            ; get current random seed
        mov.w   r12,gameSeed            ; save random seed
        mov.w   #NTONES,r6              ; counter
   
tones:  call    #rand16                 ; get random # in r12</pre></blockquote>

<!-- END LAB DESCRIPTION -->

</body>
</html>
